package com.xcjw.com.CallBack;

import android.app.ProgressDialog;
import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okhttputils.callback.AbsCallback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @packname com.homeclient.com.CallBack
 * @filename JsonCallback
 * @date on 2018/8/8 17:22
 *****/
public class JsonCallback<T>extends AbsCallback<T> {
      Class mClass;
      private Context mContext;
      private boolean        mIsShowDialog;
      private ProgressDialog progressDialog;
      private Request mRequest;

      public static Gson mGson = new Gson();
      public JsonCallback(Class targetClazz, Context context, boolean isShowDialog) {
            mClass = targetClazz;
            mContext = context;
            mIsShowDialog = isShowDialog;
      }

      public JsonCallback(Class<T> targetClazz, Context mContext) {
            this(targetClazz, mContext, false);//默认不开启动画
      }

      @Override
      public T parseNetworkResponse(Response response) throws Exception {
            T t = null;
            try {
                  //解析json,返回bean对象
                  t = (T) mGson.fromJson(response.body().string(), mClass);
            } catch (JsonSyntaxException e) {
                  e.printStackTrace();
            } catch (IOException e) {
                  e.printStackTrace();
            }
            return t;
      }

      @Override
      public void onSuccess(T t, Call call, Response response) {

      }
}